﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DALLayer.Base;
using DataDefinition;

namespace DALLayer.Providers
{
    public class ArticleTypeDataProvider : DataAccessBase
    {
         #region [Constructor]
        /// <summary>
        /// Initializes a new instance of the <see cref="ArticleTypeDataProvider" /> class.
        /// </summary>
        public ArticleTypeDataProvider() { }
        #endregion
        #region [Public Methods]
        /// <summary>
        ///  Method gets all the article types or by id
        /// </summary>
        /// <param name="articleTypeId">article type id</param>
        /// <param name="selectById">select by id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetAllArticleTypes(int articleTypeId, bool selectById)
        {
            AutoFixDataSet dataSet = null;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT ArticleType.* FROM ArticleType");
                //if its a selection by id
                if (selectById == true)
                {
                    //Add the sql string to retrieve data based on ArticleTypeID
                    base.CommandTextBuilder.Append(@" WHERE (ArticleTypeID = @ArticleTypeID)");
                    //Add the params needed to retrieve data
                    base.DBParams.Add(@"@ArticleTypeID", articleTypeId);
                }
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.ArticleType.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        /// <summary>
        ///  Method gets all the article types by article id
        /// </summary>
        /// <param name="articleId">article id</param>
        /// <returns>data set</returns>
        public AutoFixDataSet GetArticleTypeByArticleID(int articleId)
        {
            AutoFixDataSet dataSet = null;
            try
            {
                //Triggers the method in the parent class DataAccessBase
                base.InitializeDataAccess();
                //Passes the sql string that needs to be added to the property in the parent class
                base.CommandTextBuilder.Append(@"SELECT ArticleType.ArticleTypeID");
                base.CommandTextBuilder.Append(@" FROM (Article INNER JOIN
                         ArticleType ON Article.ArticleTypeID = ArticleType.ArticleTypeID)");         
                //Add the sql string to retrieve data based on ArticleTypeID
                base.CommandTextBuilder.Append(@" WHERE (Article.ArticleID = @ArticleID) AND (Article.IsActive = True)");
                //Add the params needed to retrieve data
                base.DBParams.Add(@"@ArticleID", articleId);
             
                bool success = false;
                //Check if it is success
                success = base.ExecuteReader();
                //If it is successful
                if (success == true)
                {
                    //Initial the dataset
                    dataSet = new AutoFixDataSet();
                    //Fill the table
                    dataSet.GetArticleTypeArticleID.Load(base.DataReader);
                }
            }
            catch (Exception eX)
            {
                // log the error here
            }
            finally
            {
                //Close connection
                base.FinalizeConnection();
            }
            //Return the filled dataset
            return dataSet;
        }
        #endregion

    }
}
